﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class FileUtil
{
    /// <summary>
    /// 检测文件是否存在
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <returns></returns>
    public static bool IsExsits(string path)
    {
        FileInfo fileInfo = new FileInfo(path);  //查找文件
        return fileInfo.Exists;
    }
    /// <summary>
    ///写入文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name=""></param>
    /// <param name="date"></param>
    public static void WriteFile(string path, byte[] data)
    {
        //获取标准路径
        path = PathUtil.GetStandarPath(path);
        //文件夹的路径
        string dir = path.Substring(0, path.LastIndexOf("/")); ;

        if (!Directory.Exists(dir)) //不存在
        {
            Directory.CreateDirectory(dir);
        }

        FileInfo file = new FileInfo(path);
        if (file.Exists)//手动覆盖式重写
        {
            file.Delete();
        }
        try
        {
            //文件流写入
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                fs.Write(data,0, data.Length);
                fs.Close();//关闭文件流
            }
        }
        catch(IOException e)
        {
            Debug.LogError(e.Message);
        }
    }
}
